define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'bootstrap-table-jumpto'], function ($, undefined, Backend, Table, Form, Jumpto) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/order/index' + location.search,
                    add_url: 'shop/order/add',
                    edit_url: 'shop/order/edit',
                    del_url: 'shop/order/del',
                    multi_url: 'shop/order/multi',
                    table: 'shop_order',
                }
            });

            var table = $("#table");
            $('.my-create-file').click(function () {
                var options = table.bootstrapTable('getOptions');
                var search = options.queryParams({});

                var filter = search.filter;
                var op = search.op;
                var url =  'shop/order/exportXlsx?filter=' + filter + '&op=' + op;
                url = Fast.api.fixurl(url);
                window.open(url);
            });
            table.on('load-success.bs.table', function (e, data) {
                $('.my-edit-money').on('click', function () {
                    var id = $(this).data('pk');
                    Fast.api.open('shop/order/editBalance?id=' + id, '充值余额',{
                        area: ['400px', '400px']
                    });
                });


            });
            //定时刷新
            setInterval(function () {
                $(".btn-refresh").trigger("click");
            }, 300000);

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);


                $("input[name='province']", form)
                    .data('source', Config.provincelist).data('pagination', false).data('multiple', true);

            });
            setInterval(function(){
                $(".btn-refresh").trigger("click");
            }, 20000);
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                showExport: false,
                showJumpto: true,
                searchFormVisible: true,
                onLoadSuccess: function(data) {
                    $("#sum-order").html(data.statistics);
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'province', title: '大区', visible: false,operate: "IN",addClass: 'selectpage'},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: '用户ID'},
/*
                        {field: 'user.parent_id', title: '业务员ID', extend:"data-source='user/user/index' data-params= '{\"custom[is_yewuyuan]\": 1}' data-field='username'",addClass: 'selectpage'},
*/
                        {field: 'user.parent_id', title: '业务员ID'},
                        {field: 'parent_name', title: '业务员电话', operate: false},
                        {field: 'name', title: __('Name'), operate: 'like'},
                        {field: 'mobile', title: __('Mobile'), operate: 'like'},
                        {field: 'goods.name', title: '牛名称', operate: 'like'},

                        {field: 'num', title: __('Num'), sortable: true, operate: false},
                        {field: 'company_text', title: '牛场',operate: 'like'},
                        {field: 'address_text', title: '地址',operate: false,cellStyle:Controller.api.css.area},
                        {field: 'remark', title: '备注',operate: false,cellStyle:Controller.api.css.area},
                        {field: 'type', title: __('Type'), searchList: {'0' : '待处理', '1' :'已安排', '2' :'拒绝', '3':'待定'}, formatter: Table.api.formatter.normal},
                        {field: 'money', title: '成交金额',operate:false,formatter: function (value, row, index) {
                                if (value === '暂未定价') {
                                    return  '<a href="javascript:;" class="btn my-edit-money"   data-pk="'+row.id+'"><i class="fa fa-pencil-square-o">' + value+ '</a>';
                                } else {
                                    return value;
                                }
                            }},

                        {field: 'createtime_text', title: '预定时间', operate:false},
                        {field: 'createtime', visible: false,title: '预定时间', operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'use_time', title: '库存不足使用时间', operate:'RANGE', addclass:'datetimerange', formatter: function (value, row, index) {
                            if (row.use_time > 0) {
                                return '库存不足使用时间：' +  Table.api.formatter.datetime(row.use_time)
                            }  else {
                                return '库存充足';
                            }
                        }},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [

                                {
                                    name: 'remark',
                                    text: '审核',
                                    title: '审核',
                                    classname: 'btn btn-xs btn-info btn-click',
                                    icon: 'fa fa-hand-o-up',
                                    hidden: function(row) {
                                        if (row.type == 0 || row.type== 3) {
                                            return false;
                                        } else {
                                            return true;
                                        }
                                    },
                                    click: function (event, data) {
                                        Layer.confirm("<form id='check-piliang' class='form-horizontal'  action='' method='post'>" +
                                            "<textarea class='form-control' rows='3' name='remark'></textarea>" +
                                            "</form>",
                                            {
                                                title: '审核申请',
                                                btn: ["安排处理", "决绝", '待定'],
                                                yes: function (index, layero) {
                                                    Fast.api.ajax({
                                                        url: 'shop/order/check',
                                                        data: {
                                                            id: data.id,
                                                            type: 1,
                                                            remark: $("textarea[name=remark]", layero).val(),
                                                        }
                                                    }, function (ret) {
                                                        Layer.close(index);
                                                        $(".btn-refresh").trigger("click");
                                                    });
                                                    return false;
                                                },
                                                btn2: function (index, layero) {
                                                    Fast.api.ajax({
                                                        url: 'shop/order/check',
                                                        data: {
                                                            id: data.id,
                                                            type: 2,
                                                            remark: $("textarea[name=remark]", layero).val(),
                                                        }
                                                    }, function (ret) {
                                                        Layer.close(index);
                                                        $(".btn-refresh").trigger("click");
                                                    });

                                                },
                                                btn3: function (index, layero) {
                                                    Fast.api.ajax({
                                                        url: 'shop/order/check',
                                                        data: {
                                                            id: data.id,
                                                            type: 3,
                                                            remark: $("textarea[name=remark]", layero).val(),
                                                        }
                                                    }, function (ret) {
                                                        Layer.close(index);
                                                        $(".btn-refresh").trigger("click");
                                                    });

                                                }
                                            } )
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'shop/order/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'shop/order/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'shop/order/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        editbalance: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            css: {
                area: function () {
                    return {
                        css: {
                            'max-width': '100px',
                        }
                    };
                }
            }
        }
    };
    return Controller;
});